package com.efounder.frame.activity.manager;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.core.xml.StubObject;
import com.efounder.frame.fragment.EFAppAccountFragment;
import com.efounder.frame.fragment.EFChatFragment;
import com.efounder.mobilemanager.R;
import com.efounder.util.AppContext;

import java.util.List;

/**
 * Created by XinQing on 2016/8/29.
 */

public class EFAppAccountActivityFragmentManager {
    /** fragment tag 前缀 **/
    private static final String FRAGMENT_TAG_PREFIX = "EFAppAccountActivity_fragment_tag_";

    private FragmentManager fragmentManager;
    /**此position需要做回被收后，恢复数据的处理:onSaveInstanceState**/
    private int currentPosition = -1;

    public EFAppAccountActivityFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    /**
     * 改变fragment：隐藏当前fragment，显示目标fragment
     * @param targetPosition 目标位置
     * @param isCacheFragment 是否缓存fragment
     * @return targetPosition
     */
    public Fragment showFragment(int targetPosition, List<StubObject> twoLevelStubList, boolean isCacheFragment){
        if (currentPosition == targetPosition){
            return fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
        }
        Fragment targetFragment = null;
//        Fragment testFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + targetPosition);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (isCacheFragment){
            //1.显示某个位置的fragment
            targetFragment = showFragmentRaw(fragmentTransaction,targetPosition,twoLevelStubList);
            //2.如果上个位置有fragment，隐藏掉
            Fragment currentFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
            if (currentFragment != null && !currentFragment.isHidden()) {// 2.2隐藏当前显示的Fragment
                fragmentTransaction.hide(currentFragment);
            }
        }else {
            //如果不缓存，直接replace
            if (twoLevelStubList == null){//消息对应的Fragment没有配置在xml中
                try {
                    targetFragment = (Fragment) Class.forName(AppContext.getInstance().getResources()
                            .getString(R.string.PUBLIC_CHAT_FRAGMENT_PACKAGE_NAME)).newInstance();
                } catch (Exception e) {
                    targetFragment = new EFChatFragment();
                    e.printStackTrace();
                }
            }else {
                targetFragment = EFAppAccountFragment.newInstance(targetPosition,twoLevelStubList);
            }
            fragmentTransaction.replace(R.id.fragment_container,targetFragment,FRAGMENT_TAG_PREFIX + targetPosition);
        }

        fragmentTransaction.commit();
//        testFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
        //最后，记录position
        currentPosition = targetPosition;
        return targetFragment;
    }
    /**
     * raw方法
     * @param fragmentTransaction
     * @param position
     */
    private Fragment showFragmentRaw(FragmentTransaction fragmentTransaction, int position, List<StubObject> twoLevelStubList){
        //1.显示某个位置的fragment
        Fragment willShowFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + position);
        if (willShowFragment == null) {// 如果没有实例化Fragment，则创建
            //如果不缓存，直接replace
            if (twoLevelStubList == null){//消息对应的Fragment没有配置在xml中
                try {
                    willShowFragment = (Fragment) Class.forName(AppContext.getInstance().getResources()
                            .getString(R.string.PUBLIC_CHAT_FRAGMENT_PACKAGE_NAME)).newInstance();
                } catch (Exception e) {
                    willShowFragment = new EFChatFragment();
                    e.printStackTrace();
                }
            }else {
                willShowFragment = EFAppAccountFragment.newInstance(position,twoLevelStubList);
            }
            fragmentTransaction.add(R.id.fragment_container,willShowFragment,FRAGMENT_TAG_PREFIX + position);
        } else if(willShowFragment.isHidden()){// 如果已经实例化，则显示
            fragmentTransaction.show(willShowFragment);
        }
        return  willShowFragment;
    }


    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }


}
